/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.basics.bean.impl;

import java.util.Arrays;

import org.springframework.util.ObjectUtils;

import com.genia.toolbox.basics.bean.EqualityMethod;

/**
 * Implementation of {@link com.genia.toolbox.basics.bean.Pair} that does not
 * consider the order of the element for the equality.
 * 
 * @param <T1>
 *          the type of the first element of the pair
 * @param <T2>
 *          the type of the second element of the pair
 */
public abstract class AbstractUnOrderedPair<T1, T2>
    extends AbstractPair<T1, T2>
{

  /**
   * constructor.
   * 
   * @param equalityMethod
   *          the {@link EqualityMethod} to use
   */
  protected AbstractUnOrderedPair(EqualityMethod equalityMethod)
  {
    super(equalityMethod);
  }



  /**
   * Indicates whether some other object is "equal to" this one.
   * 
   * @param obj
   *          the reference object with which to compare.
   * @return <code>true</code> if this object is the same as the obj argument;
   *         <code>false</code> otherwise.
   * @see #hashCode()
   * @see java.util.Hashtable
   */
  @Override
  public boolean equals(final Object obj)
  {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (obj instanceof AbstractUnOrderedPair) {
      AbstractUnOrderedPair<?, ?> other = (AbstractUnOrderedPair<?, ?>) obj;
      return (equalityMethod.isEqual(this.getFirstElement(), other.getFirstElement()) && equalityMethod.isEqual(this.getSecondElement(), other.getSecondElement()))
          || (equalityMethod.isEqual(this.getSecondElement(), other.getFirstElement()) && equalityMethod.isEqual(this.getFirstElement(), other.getSecondElement()));
    }
    return false;
  }



  /**
   * Returns a hash code value for the object. This method is supported for the
   * benefit of hashtables such as those provided by {@link java.util.Hashtable}.
   * 
   * @return a hash code value for this object.
   * @see java.lang.Object#equals(java.lang.Object)
   * @see java.util.Hashtable
   */
  @Override
  public int hashCode()
  {
    int hash1 = equalityMethod.getHash(getFirstElement());
    int hash2 = equalityMethod.getHash(getSecondElement());
    if (hash1 < hash2) {
      int tmp = hash1;
      hash1 = hash2;
      hash2 = tmp;
    }
    return ObjectUtils.nullSafeHashCode(Arrays.asList(hash1, hash2));
  }
}
